package org.xclcharts.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import org.xclcharts.common.SysinfoHelper;

public abstract class MySurfaceView extends SurfaceView implements
        SurfaceHolder.Callback {

    private DrawThread mThread = null;

    public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MySurfaceView(Context context) {
        super(context);
        init();
    }

    private void init() {
        Log.d("FSKFKS", "init");

        getHolder().addCallback(this);
//禁用硬件加速
        disableHardwareAccelerated();
        mThread = new DrawThread();

    }

    /**
     * 刷新图表
     */
    public void refreshChart() {
        this.invalidate();
    }

    /**
     * 绘制图表
     *
     * @param canvas 画布
     */
    public abstract void render(Canvas canvas);

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d("FSKFKS", "surfaceCreated");
        mThread.setRun(true);
        mThread.start();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        Log.d("FSKFKS", "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d("FSKFKS", "surfaceDestroyed");
        mThread.setRun(false);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }


    private int measure(int measureSpec) {
        int result = 100;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) { //fill_parent
            result = specSize;
        } else if (specMode == MeasureSpec.AT_MOST) { //wrap_content
            result = Math.min(result, specSize);
        }
        return result;
    }

    private int measureWidth(int measureSpec) {
        return measure(measureSpec);
    }

    private int measureHeight(int measureSpec) {
        return measure(measureSpec);
    }

    /**
     * 禁用硬件加速.
     * 原因:android自3.0引入了硬件加速，即使用GPU进行绘图,但它并不能完善的支持所有的绘图，
     * 通常表现为内容(如Rect或Path)不可见，异常或渲染错误。所以类了保证图表的正常显示，强制禁用掉.
     */
    protected void disableHardwareAccelerated() {
        //View.isHardwareAccelerated()
        //Canvas.isHardwareAccelerated()

        if (SysinfoHelper.getInstance().supportHardwareAccelerated()) {
            //是否开启了硬件加速,如开启将其禁掉
            if (!isHardwareAccelerated()) {
                //setLayerType(View.LAYER_TYPE_NONE,null);  //LAYER_TYPE_SOFTWARE
                setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            }
        }
    }

    /**
     * 绘制线程类
     */
    public class DrawThread extends Thread {
        private boolean isRun = false;

        public DrawThread() {

        }

        public void setRun(boolean isRun) {
            Log.d("FSKFKS", "DrawThread setRun: " + isRun);
            this.isRun = isRun;
        }

        @Override
        public void run() {
            Log.d("FSKFKS", "DrawThread run");
            int count = 0;

            while (isRun) {
                Canvas canvas = getHolder().lockCanvas();
                synchronized (getHolder()) {
                    try {
                        Log.d("FSKFKS", "Drawing-------------");
                        try {
                            render(canvas);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            Log.e("TSF", e.toString());
                        }
                        Thread.sleep(100);// 睡眠时间为1秒

                    } catch (Exception e) {
                        e.printStackTrace();

                    } finally {
                        if (null != canvas) {
                            getHolder().unlockCanvasAndPost(canvas);
                        }
                    }

                }

            }
        }

    }

}